package com.bjpowernode.boss.mvc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.common.constant.SysConstans;
import com.bjpowernode.boss.convert.PaymentConvert;
import com.bjpowernode.boss.dao.entity.PayProduct;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.AjaxResult;
import com.bjpowernode.boss.mvc.models.form.payproduct.PayProductForm;
import com.bjpowernode.boss.mvc.models.pages.PageInfo;
import com.bjpowernode.boss.mvc.service.PayProductService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 支付产品
 *
 * @author yu
 * @date 2024/7/11  18:06
 */
@CrossOrigin
@RequestMapping("/payment/product")
@RestController
public class PayProductController extends BaseController {

    @Resource
    private PaymentConvert paymentConvert;

    @Resource
    private PayProductService payProductService ;

    @GetMapping("/pageList")
    public AjaxResult pageList(@RequestParam Integer pageNo) {
        // 1.合法化pageNo
        pageNo = defPageNo(pageNo) ;

        // 2.分页查询
        IPage<PayProduct> pageResult = payProductService.pageList(pageNo, SysConstans.PAGE_SIZE) ;

        // 3.获得分页参数
        // 总页数
        long totalPage = pageResult.getPages() ;
        // 分页查询的记录（list）
        List<PayProduct> payProductList = pageResult.getRecords() ;

        // 4.创建分页对象
        /*
            PageInfo {
                private Long current ;
                private Long totalPage ;
            }

         */
        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), totalPage) ;

        // 5.包装发给前端的数据
        Map<String, Object> data = new HashMap<>() ;
        data.put("pageInfo", pageInfo) ;
        data.put("list" , paymentConvert.toListPayProductVo(payProductList)) ;

        return AjaxResult.OK(data) ;
    }

    @PostMapping("/add")
    public AjaxResult addPayProduct(@RequestBody @Valid PayProductForm payProductForm) {
        int add = payProductService.addPayProduct(payProductForm) ;

        return add >= 1 ? AjaxResult.OK() : AjaxResult.FAIL() ;
    }


    /**
     * 支付产品字典列表
     * @return
     */
    @GetMapping("/dicList")
    public AjaxResult queryProductDicList() {
        List<DicEntity> productDicList = payProductService.queryProductDicList() ;
        return AjaxResult.OK(productDicList) ;
    }
}
